/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.candidat;

import gestionform.GestionCandidat;
import gestionform.GestionCandidatures;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Administrateur;
import model.Candidat;
import model.Candidature;

/**
 *
 * @author Paul
 */
public class ServletCandidats extends HttpServlet {

    public static final String VUE_LISTE_CANDIDATS = "/WEB-INF/listeCandidats.jsp";
    private static final String ATT_GESTION_CANDIDATS = "gCandidats";

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Administrateur a = (Administrateur) request.getSession().getAttribute("administrateur");
        if (a != null) {
            if (request.getSession().getAttribute("listeCandidats") == null) {
                HttpSession session = request.getSession(true);
                GestionCandidat gCandidats = new GestionCandidat(a);
                GestionCandidatures gCandidatures = new GestionCandidatures();
                List<Candidat> listeCandidats;
                //List<Candidature> listeCandidatures = null;
                HashMap<Candidat, List<Candidature>> map = new HashMap<Candidat, List<Candidature>>();
                try {
                    listeCandidats = gCandidats.getListeCandidats();
                    for (Candidat c : listeCandidats) {
                        Candidat cInfos = gCandidatures.getInfosCandidat(c);
                        map.put(cInfos, gCandidatures.getAllCandidatures(c));
                    }
                    session.setAttribute("listeCandidats", map);
                } catch (Exception ex) {
                    Logger.getLogger(ServletCandidatures.class.getName()).log(Level.SEVERE, null, ex);
                }
                request.setAttribute(ATT_GESTION_CANDIDATS, gCandidats);
            }
            request.getRequestDispatcher(VUE_LISTE_CANDIDATS).forward(request, response);

        } else {
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Administrateur a = (Administrateur) request.getSession().getAttribute("administrateur");
        if (a != null) {
            if (request.getSession().getAttribute("listeCandidats") == null) {
                HttpSession session = request.getSession(true);
                GestionCandidat gCandidats = new GestionCandidat(a);
                GestionCandidatures gCandidatures = new GestionCandidatures();
                List<Candidat> listeCandidats;
                //List<Candidature> listeCandidatures = null;
                HashMap<Candidat, List<Candidature>> map = new HashMap<Candidat, List<Candidature>>();
                try {
                    listeCandidats = gCandidats.getListeCandidats();
                    for (Candidat c : listeCandidats) {
                        Candidat cInfos = gCandidatures.getInfosCandidat(c);
                        map.put(cInfos, gCandidatures.getAllCandidatures(c));
                    }
                    session.setAttribute("listeCandidats", map);
                } catch (Exception ex) {
                    Logger.getLogger(ServletCandidatures.class.getName()).log(Level.SEVERE, null, ex);
                }
                request.setAttribute(ATT_GESTION_CANDIDATS, gCandidats);
            }
            request.getRequestDispatcher(VUE_LISTE_CANDIDATS).forward(request, response);

        } else {
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
    }
}
